{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T13:41:14.811611Z",
     "start_time": "2025-01-06T13:41:14.808480Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "import time\n",
    "import random"
   ],
   "id": "f4f005d41357e0ca",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 对比",
   "id": "e8d6c4db2400ea84"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T13:32:18.451124Z",
     "start_time": "2025-01-06T13:32:17.801943Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a=[]\n",
    "for i in range(10**6):\n",
    "    a.append(random.randint(0,500))\n",
    "b=np.array(a)\n",
    "t1=time.time()\n",
    "ave1=max(a)\n",
    "t2=time.time()\n",
    "print(t2-t1)\n",
    "t3=time.time()\n",
    "ave2=np.max(b)\n",
    "t4=time.time()\n",
    "print(t4-t3)"
   ],
   "id": "5aea6221047f0b18",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.018939971923828125\n",
      "0.000997304916381836\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T13:57:45.908808Z",
     "start_time": "2025-01-06T13:57:45.903934Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list_1= [[1, 2], [3, 4], [5, 6]]\n",
    "ny_1=np.array(list_1)\n",
    "ny_2=ny_1\n",
    "print(ny_1)\n",
    "print(ny_1.shape)\n",
    "print(ny_1.size)\n",
    "print(ny_1.ndim)\n",
    "print(ny_1.dtype)\n",
    "ny_1.shape=(2,3)#修改原有的数组形状\n",
    "print(ny_1)\n",
    "ny_3=ny_1.reshape(2,3)#重新形成新的数组\n",
    "print(ny_3)\n",
    "print(id(ny_1),id(ny_2),id(ny_3))\n",
    "ny_4=ny_1.flatten()#转换为一维数组\n",
    "print(ny_4)\n",
    "ny_5=ny_4.reshape(2,3)#转换为原来的形状\n",
    "print(ny_5)\n",
    "tolist=ny_5.tolist()#转换为列表\n",
    "print(tolist)\n",
    "\n"
   ],
   "id": "f71168c7e620ee0b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n",
      "(3, 2)\n",
      "6\n",
      "2\n",
      "int64\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "2124955148688 2124955148688 2125342909392\n",
      "[1 2 3 4 5 6]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1, 2, 3], [4, 5, 6]]\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T14:07:30.652108Z",
     "start_time": "2025-01-06T14:07:30.645849Z"
    }
   },
   "cell_type": "code",
   "source": [
    "f = np.array([1,2,3,4,5], dtype = np.int16)\n",
    "print(f.dtype)\n",
    "g = f.astype(np.float32)#类型转换\n",
    "print(g.dtype)\n",
    "print(round(random.random(),2))#保留2位小数\n",
    "t1 =np.arange(24).reshape((6,4))#运用广播技术，所有数组元素都做相同的操作\n",
    "print(t1+2)\n",
    "print(\"*\"*50)\n",
    "print(t1*2)\n",
    "t2=t1+2\n",
    "print(\"*\"*50)\n",
    "print(t2)\n",
    "print(\"*\"*50)\n",
    "print(t1+t2)#两个相同形状的数组进行运算，是对应元素的运算\n",
    "t1 = np.arange(24).reshape((4,6))\n",
    "t2 = np.arange(4).reshape((4,1))#不同形状的数组进行运算，是广播的概念，将第二个数组广播到第一个数组的形状，但必须有一个数组的维度为1，且维度相同\n",
    "print(\"*\"*50)\n",
    "print(t1-t2)\n"
   ],
   "id": "eed80f2f0cb286dd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "int16\n",
      "float32\n",
      "0.31\n",
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "**************************************************\n",
      "[[ 0  2  4  6]\n",
      " [ 8 10 12 14]\n",
      " [16 18 20 22]\n",
      " [24 26 28 30]\n",
      " [32 34 36 38]\n",
      " [40 42 44 46]]\n",
      "**************************************************\n",
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]\n",
      " [14 15 16 17]\n",
      " [18 19 20 21]\n",
      " [22 23 24 25]]\n",
      "**************************************************\n",
      "[[ 2  4  6  8]\n",
      " [10 12 14 16]\n",
      " [18 20 22 24]\n",
      " [26 28 30 32]\n",
      " [34 36 38 40]\n",
      " [42 44 46 48]]\n",
      "**************************************************\n",
      "[[ 0  1  2  3  4  5]\n",
      " [ 5  6  7  8  9 10]\n",
      " [10 11 12 13 14 15]\n",
      " [15 16 17 18 19 20]]\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 切片和索引",
   "id": "18e662496a7bd391"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-06T14:21:53.837685Z",
     "start_time": "2025-01-06T14:21:53.831475Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(10)\n",
    "# 冒号分隔切片参数 start:stop:step 来进行切片操作print(a[2:7:2])# 从索引 2 开始到索引 7 停止，间隔为 2\n",
    "# 如果只放置一个参数，如 [2]，将返回与该索引相对应的单个元素\n",
    "print(a[0], a)\n",
    "# 如果为 [2:]，表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2:])\n",
    "print(a[2:8:2])  #切片是左闭右开的，即右边的索引不包含在切片内，左边的索引包含在切片内\n",
    "b=np.arange(20).reshape(4,5)\n",
    "print(b[:,1:4])#取1-4列\n",
    "print(b[0:2,:])#取0-1行（右边的2不包括，是开区间\n",
    "print(b[1:,1:4])#取1-结尾行1-4列\n",
    "print(b[[1,3,0]])#依次取1,3,0行\n",
    "b[b<10]=0#将b中小于10的元素赋值为0\n",
    "print(\"*\"*50)\n",
    "print(b)\n",
    "b[1:3]=0#将1-2行的元素赋值为0\n",
    "print(b)\n",
    "b[0:3,0:3]=10#将0-2行0-2列的元素赋值为10\n",
    "print(b)\n",
    "\n",
    "\n"
   ],
   "id": "ab691f44a21ee9c6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [0 1 2 3 4 5 6 7 8 9]\n",
      "[2 3 4 5 6 7 8 9]\n",
      "[2 4 6]\n",
      "[[ 1  2  3]\n",
      " [ 6  7  8]\n",
      " [11 12 13]\n",
      " [16 17 18]]\n",
      "[[0 1 2 3 4]\n",
      " [5 6 7 8 9]]\n",
      "[[ 6  7  8]\n",
      " [11 12 13]\n",
      " [16 17 18]]\n",
      "[[ 5  6  7  8  9]\n",
      " [15 16 17 18 19]\n",
      " [ 0  1  2  3  4]]\n",
      "**************************************************\n",
      "[[ 0  0  0  0  0]\n",
      " [ 0  0  0  0  0]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]]\n",
      "[[ 0  0  0  0  0]\n",
      " [ 0  0  0  0  0]\n",
      " [ 0  0  0  0  0]\n",
      " [15 16 17 18 19]]\n",
      "[[10 10 10  0  0]\n",
      " [10 10 10  0  0]\n",
      " [10 10 10  0  0]\n",
      " [15 16 17 18 19]]\n"
     ]
    }
   ],
   "execution_count": 41
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
